package com.jzg.jcptadmin.adpter;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.data.vo.ChooseResultData;

import java.util.List;

/**
 * author: guochen
 * date: 2016/11/8 16:09
 * email:
 */
public class ChooseResultItemAdapter extends BaseAdapter {
    private Context context;
    private List<ChooseResultData.DataBean.GroupValueBean> datas;
    private ViewHolder viewHolder;

    public ChooseResultItemAdapter(Context context, List<ChooseResultData.DataBean.GroupValueBean> datas) {
        this.context = context;
        this.datas = datas;
    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.choose_result_item, null);
            viewHolder.result = (TextView) convertView.findViewById(R.id.tv_result);
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
            viewHolder.ll_linear = (RelativeLayout) convertView.findViewById(R.id.ll_linear);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
//设置数据
        viewHolder = (ViewHolder) convertView.getTag();
//        viewHolder.result.setText(datas.get(position).getItemValue());
        SpannableStringBuilder styledText = new SpannableStringBuilder(datas.get(position).getItemValue());
        for(int i=0;i<datas.get(position).getRedWord().size() ;i++){
            styledText.setSpan(new ForegroundColorSpan(Color.parseColor("#ff7701")),datas.get(position).getRedWord().get(i).getStart()
                    , datas.get(position).getRedWord().get(i).getStart() + datas.get(position).getRedWord().get(i).getLength(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        viewHolder.result.setText(styledText);
        viewHolder.checkBox.setChecked(datas.get(position).isSelect());
        if (datas.get(position).isSelect()) {
            viewHolder.ll_linear.setBackgroundResource(R.drawable.btn_result_sel);
        } else {
            viewHolder.ll_linear.setBackgroundResource(R.drawable.btn_result_nor);
        }

        return convertView;
    }

    public class ViewHolder {

        public TextView result;
        public CheckBox checkBox;
        public RelativeLayout ll_linear;

    }

}
